package com.mxpio.webapp.controller;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ZipUtil;
import com.alibaba.fastjson.JSONObject;
import com.mxpio.webapp.dto.BaseResponsePackage;
import com.mxpio.webapp.dto.CiphertextPackage;
import com.mxpio.webapp.dto.Result;
import com.mxpio.webapp.dto.ServerPackage;
import com.mxpio.webapp.service.IMonitorServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;

/**
 * @author: Cheng
 * @date: 2022/11/4
 */
@RestController
@RequestMapping("/server")
public class MonitorController {

    @Autowired
    private IMonitorServerService monitorServerService;

    @PostMapping("/accept-server-package")
    public BaseResponsePackage acceptServerPackage(@RequestBody CiphertextPackage ciphertextPackage) {
        // 解密后的字符串
        String decryptStr = ciphertextPackage.getCiphertext();
        // 是否需要进行UnGzip
        boolean isUnGzipEnabled = ciphertextPackage.isUnGzipEnabled();
        if (isUnGzipEnabled) {
            // 解密
            byte[] decrypt = Base64.getDecoder().decode(decryptStr);
            // 解压
            decryptStr = ZipUtil.unGzip(decrypt, CharsetUtil.UTF_8);
        }
        monitorServerService.dealServerPackage(decryptStr);
        BaseResponsePackage baseResponsePackage = new BaseResponsePackage();
        baseResponsePackage.setResult(new Result(true,"monitor"));

        return baseResponsePackage;
    }
}
